#include "server.h"
#include "ui_server.h"
#include "mysql.h"


Server::Server(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::Server) {
	qDebug() << "到达服务器" << endl;
	ui->setupUi(this);
	this->server = new QTcpServer(this);
	this->socket = new QTcpSocket(this);
    int port = 8888;
    qDebug()<<server->listen(QHostAddress::Any, port); //监听本机的某个端口
	//连接信号槽
	connect(server, SIGNAL(newConnection()), this, SLOT(Connect())); //接受来自客户端的信号并连接
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),  this, SLOT(displayError(QAbstractSocket::SocketError)));
//    connect(socket,SIGNAL(readyRead()),this,SLOT(ReadMSG())); //连接成功则引发槽函数
//	connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(doubleclicked(QListWidgetItem*))); //双击触发事件

	//加载用户列表
	QString str = NULL;
	query = new QSqlQuery;
	query->exec("select name from user ");

	while (query->next()) {
		QString str = query->value(0).toString();
		QListWidgetItem *item = new QListWidgetItem;
		item->setText(str);      //设置列表项的文本
		ui->listWidget->addItem(item);     //加载列表项到列表框
	}



}

Server::~Server() {
	server->close();
	delete ui;
}

int Server::checkLogIn(QString Name, QString Password) {//检查登陆
	MySql *sql = new MySql();
	int ret = sql->Login(Name, Password);
	return ret;
}

bool Server::checkSignUp(QString Name, QString Password, QString Mail,QString headpic) {//检查注册
	MySql *sql = new MySql();
    bool ret = sql->Regist(Name, Password, Mail,headpic);
	return ret;
}

bool Server::saveInformation(QString Name, QString Password, QString Mail,QString headpic) {//保存修改的个人信息
    MySql *sql = new MySql();
    bool ret = sql->saveInformation(Name, Password, Mail,headpic);
    return ret;
}

int Server::findpwd(QString Name, QString Mail) {//找回密码
	MySql *sql = new MySql();
	int temp = sql->findpwd(Name, Mail);
	return temp;
}

int Server::findinf(QString Name) {//找到个人信息
    MySql *sql = new MySql();
    int temp = sql->findinf(Name);
    return temp;
}

void Server::Connect() { //连接客户端和服务端
	socket = server->nextPendingConnection();
	qDebug() << "连接两端" << endl;
	connect(socket, SIGNAL(readyRead()), this, SLOT(ReadMSG())); //获取来自客户端的数据
}

void Server::ReadMSG() { //读取来自客户端的数据
	qDebug() << "读取客户端数据" << endl;
	QString data = socket->readAll();   //读取客户端发送的数据
	qDebug() << data;
	QStringList list = data.split("#");
	bool ret = 0;
	int temp = 0;
	QString sendData = list[0];
    QString password, mail,headpic,str1, str2, str3, str4;
	if (list[0] == "a") { //注册
		qDebug() << "到达注册" << endl;
        ret = checkSignUp(list[1], list[2], list[3],list[4]);
		if (ret)
			sendData = sendData + "#true";
		else
			sendData = sendData + "#false";
		socket->write(sendData.toLatin1());
	} else if (list[0] == "b") { //登录
		temp = checkLogIn(list[1], list[2]);
		if (temp == 1) {
			query = new QSqlQuery;
            query->exec(QString("update user set status='%1' where Name='%2'").arg(1).arg(list[1])); //设置数据库中的status为1
			sendData = sendData + "#" + list[1] + "#true";
		} else if (temp == 0)  sendData = sendData + "#false" + "#0";
		else if (temp == 2)  sendData = sendData + "#false" + "#2";
		else if (temp == 3)  sendData = sendData + "#false" + "#3";
		socket->write(sendData.toUtf8());

	} else if (list[0] == "c") { //找回密码
		temp = findpwd(list[1], list[2]);
		if (temp == 0) sendData = sendData + "#false" + "#0";
		else if (temp == 1) sendData = sendData + "#false" + "#1";
		else if (temp == 2) sendData = sendData + "#false" + "#2";
		else if (temp == 4) sendData = sendData + "#false" + "#4";
		else {
            QString str = QString("select Passwords from user where Name='%1'").arg(list[1]);
			query = new QSqlQuery;
			query->exec(str);
			while (query->next()) {
				password = query->value(0).toString();
			}
			sendData = sendData + "#true" + "#" + password;
		}
		socket->write(sendData.toUtf8());
	} else if (list[0] == "d") { //退出登录
		query = new QSqlQuery;
        query->exec(QString("update user set status='%1' where Name='%2'").arg(0).arg(list[1])); //设置数据库中的status为0

	} else if (list[0] == "e") { //返回用户列表
       // query->exec(QString("update user set status='%1' where Name='%2'").arg(1).arg(list[1])); //设置数据库中的status为1
        query = new QSqlQuery;
        query->exec(QString("update user set CIP='%1' , CPort='%2' where Name='%3'").arg(list[2]).arg(list[3]).arg(list[1]));
        qDebug() << list[2] << list[3] << list[1] << "6666666666666666666666666" << endl;
        query = new QSqlQuery;
        query->exec("select Name,status,Mail2 from user");
        QString str6 = NULL;

        while (query->next()) {
            str1 = query->value(0).toString();
            str6 = str6 + "#" + str1;
            str1 = query->value(1).toString();
            str6 = str6 + "#" + str1;
            str1 = query->value(2).toString();
            str6 = str6 + "#" + str1;
        }
		query = new QSqlQuery;
		query->exec(QString("select name1,MSG from msg where name2='%1'").arg(list[1]));
		while (query->next()) {
			str3 = query->value(0).toString();
			str4 = query->value(1).toString();
		}
		if (str4 != NULL) {
			str3 = str3 + ":" + str4;
		}
		sendData = sendData + str6 + "#" + "end" + "#" + str3;
		socket->write(sendData.toUtf8());
	} else if (list[0] == "f") { //群聊消息
		qDebug() << "群聊";
		//获取系统时间
		QDateTime time = QDateTime::currentDateTime();
		QString Time = time.toString("yyyy-MM-dd ddd hh:mm:ss");
		ui->textBrowser->append(Time);
		ui->textBrowser->append(list[1] + ":" + list[2]);
		sendData = sendData + "#" + list[1] + "#" + list[2];
		query = new QSqlQuery;
		int str5;
		qDebug() << "群聊      1";
		query->exec(QString("select CIP, CPort from user where status = '%1'").arg(1));
		while (query->next()) {

			QString str1 = query->value(0).toString();
            str5 = query->value(1).toString().toInt();
            qDebug() << str5;
			socket = new QTcpSocket();
			socket->abort();   //取消已有的连接
			socket->connectToHost(str1, str5); //连接到客户端
			socket->waitForConnected();
            qDebug() << sendData + "  "+ str1 + "   " +str5;
            socket->write(sendData.toUtf8());

		}
	} else if (list[0] == "g") { //私聊
		qDebug() << "到达服务器的私聊";
		QString Status;
		query = new QSqlQuery;
        query->exec(QString("select status from user where Name='%1'").arg(list[1]));
		while (query->next()) {
			Status = query->value(0).toString();
		}
		if (Status == "1") {
            query->exec(QString("select CIP, CPort from user where Name = '%1'").arg(list[1]));
			while (query->next()) {
				str1 = query->value(0).toString();
				str2 = query->value(1).toString();
			}
			sendData = sendData + "#" + list[1] + "#" + "1" + "#" + str1 + "#" + str2;
		} else {
			sendData = sendData + "#" + list[1] + "#" + "0";
		}
		socket->write(sendData.toUtf8());

    }  else if (list[0] == "h") { //找到个人信息
        temp = findinf(list[1]);
        if (temp == 3) {
            QString str = QString("select Passwords,Mail1,Mail2 from user where Name='%1'").arg(list[1]);
            query = new QSqlQuery;
            query->exec(str);
            //query->exec(str2);

            while (query->next()) {
                password = query->value(0).toString();
                mail = query->value(1).toString();
                headpic = query->value(2).toString();

            }


            sendData = sendData + "#true" + "#" + password+"#"+mail+"#"+headpic;
        }
        socket->write(sendData.toUtf8());
    }if (list[0] == "j") { //保存个人信息
        qDebug() << "到达保存" << endl;
        ret = saveInformation(list[1], list[2], list[3],list[4]);
        if (ret)
            sendData = sendData + "#true";
        else
            sendData = sendData + "#false";
        socket->write(sendData.toLatin1());
    }

    else if (list[0] == "s") { //获取信息以发送文件

        query->exec(QString("select CIP, CPort from user where Name = '%1'").arg(list[1]));
		while (query->next()) {
			str1 = query->value(0).toString();
			str2 = query->value(1).toString();
		}
		sendData = sendData + "#" + list[1] + "#" + "1" + "#" + str1 + "#" + str2;
		socket->write(sendData.toUtf8());
	} else if (list[0] == "i") { //离线消息
		query = new QSqlQuery;
		query->exec(QString("insert into msg (name1, name2, MSG) values('%1','%2','%3')").arg(list[1]).arg(list[2]).arg(list[3]));
	} else if (list[0] == "y") { //获取信息以发送私聊信息

        query->exec(QString("select CIP, CPort from user where Name = '%1'").arg(list[1]));
		while (query->next()) {
			str1 = query->value(0).toString();
			str2 = query->value(1).toString();
		}
		sendData = sendData + "#" + list[1] + "#" + "1" + "#" + str1 + "#" + str2;
		socket->write(sendData.toUtf8());
	} else
		return;
}

void Server::on_pushButton_Flash_clicked() {
	//刷新用户列表
	QString str = NULL;
	query = new QSqlQuery;
    query->exec("select Name, status from user ");
	ui->listWidget->clear();
	while (query->next()) {
		QString str = query->value(0).toString();
		QString sss = query->value(1).toString();
		QListWidgetItem *item = new QListWidgetItem;
		if (sss == '1') {
            item->setText(str + " (在线)");      //设置列表项的文本
		} else {
            item->setText(str + " (下线)");      //设置列表项的文本
		}
		ui->listWidget->addItem(item);     //加载列表项到列表框
	}
}
